/**
 * 
 */
package org.shark.jdbcTemplateTool.utils.sql;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.shark.jdbcTemplateTool.exception.SqlFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author lenovo
 * @description
 * @date 2016年8月15日
 */
public class SqlXmlUtil extends AbstractBaseSqlLoad implements SqlLoad {

    /**
	 * 
	 */
	private static final String FILE_EXT = ".sql.xml";

	/**
	 * 
	 */
	protected static final Logger log = LoggerFactory.getLogger(SqlXmlUtil.class);
	
	/**
	 * 
	 */
	public static SqlXmlUtil newInstance = new SqlXmlUtil();

	
    /**
     * 初始化加载sql语句到map
     * @throws SqlFormatException 
     */
    public synchronized void init(boolean isInit, String cacheName) throws SqlFormatException {
    	
    	List<File> files = scanFiles(cacheName);
        
        SAXReader reader = new SAXReader();
        String fileName = null;
        try {
            for (File xmlfile : files) {
                fileName = xmlfile.getName();
                Document doc = reader.read(xmlfile);
                Element root = doc.getRootElement();
                String namespace = root.attributeValue("namespace");
                if(null == namespace || namespace.trim().isEmpty()){
                	throw new SqlFormatException("sql xml文件" + fileName + "的命名空间不能为空");
                }
                
                for(Iterator<?> iterTemp = root.elementIterator(); iterTemp.hasNext();) {   
                    Element element = (Element) iterTemp.next();    
                    if(element.getName().toLowerCase().equals("sql")){
                        String id = element.attributeValue("id");
                        if(null == id || id.trim().isEmpty()){
                        	throw new SqlFormatException("sql xml文件" + fileName + "的存在没有id的sql语句");
                        }
                        
                        String sql = element.getText();
                        if(null == sql || sql.trim().isEmpty()){
                        	throw new SqlFormatException("sql xml文件" + fileName + "的存在没有内容的sql语句");
                        }
                        
                        String key = namespace + "." + id;
                        if(isInit && containsKey(key)){
                        	throw new SqlFormatException("sql xml文件" + fileName + "的sql语句" + key + "的存在重复命名空间和ID");
                        } else if(containsKey(key)){
                        	throw new SqlFormatException("sql xml文件" + fileName + "的sql语句" + key + "的存在重复命名空间和ID");
                        }
                        
                        sql = sql.replaceAll("[\\s]{2,}", " ");
                        putCache(key, sql);
                        log.debug("sql加载, sql file = " + fileName + ", sql key = " + key + ", sql content = " + sql);
                    }
                }
            }
        } catch (DocumentException e) {
        	throw new SqlFormatException("sql xml文件" + fileName + "解析异常", e);
        }
    }
    
    protected String getFileExt() {
    	return FILE_EXT;
    }    
}
